/*
 * ========================================================
 * Copyright(c) 2014 杭州龙骞科技-版权所有
 * ========================================================
 * 本软件由杭州龙骞科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * http://www.hzdracom.com/
 * ========================================================
 */
package com.shwread.android.qysw.activity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.WindowManager;

import com.ciko.code.bean.UserBean;
import com.humanres.activity.ResMoreActivity;
import com.humanres.activity.ResOtherActivity;
import com.mywork.activity.WorkMeetingActivity;
import com.shwread.android.qysw.activity.healthy.FriendPKDetailActivity;
import com.shwread.android.qysw.activity.healthy.GroupPKDetailActivity;
import com.shwread.android.qysw10000204.R;
import com.shwread.httpsdk.http.callback.ActionListener;
import com.shwread.httpsdk.http.face.QryMsgByIdAction;
import com.tgx.tina.android.ipc.framework.BaseActivity;

import logic.bean.MessageInfo;
import logic.constants.Const;
import logic.constants.DefaultConsts;
import logic.dao.UserDao;
import logic.util.Util;


/**
 * @Project： Shwread
 * @Title：BubbleActivity
 * @Description：
 * @Author：wayne
 * @Date：2015年4月30日下午2:25:43
 */
public class BubbleActivity
        extends
        BaseActivity
        implements
        ActionListener {

    private MessageInfo info = null;
    private Context context;
    private boolean isRunning;

    private Handler myHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    if (null != info) {
                        toShelfActivity(info);
                    }
                    break;
                case 2:
                    BubbleActivity.this.finish();
                    break;
                default:
                    BubbleActivity.this.finish();
                    break;
            }
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 0.0f;
        getWindow().setAttributes(lp);
        setContentView(R.layout.bubble_lay);
        context = this;
        String msgId = getIntent().getStringExtra(DefaultConsts.message_id);
        int msgType = getIntent().getIntExtra(DefaultConsts.message_type, -1);
        Intent extras = new Intent();
        switch (msgType) {
            case MessageInfo.TYPE_QIANDAO:// 签到提醒
                extras.setComponent(new ComponentName(context, MainActivity.class));
                extras.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(extras);
                break;
            case MessageInfo.TYPE_QINGJIA:// 请假提醒
                extras.setClass(context, ResMoreActivity.class);
                extras.putExtra("title", "请假管理");
                extras.putExtra("push", true);
                startActivity(extras);
                finish();
                break;
            case MessageInfo.TYPE_CHUCAI:// 出差提醒
                extras.setClass(context, ResMoreActivity.class);
                extras.putExtra("title", "出差管理");
                extras.putExtra("push", true);
                startActivity(extras);
                finish();
                break;
            case MessageInfo.TYPE_JIABAN:// 加班提醒
                extras.setClass(context, ResMoreActivity.class);
                extras.putExtra("title", "加班管理");
                extras.putExtra("push", true);
                startActivity(extras);
                finish();
                break;

            case 17://公文提醒
                extras.setClass(context, ResOtherActivity.class);
                extras.putExtra("title", "公文传阅");
                extras.putExtra("push", true);
                startActivity(extras);
                finish();
                break;
            case 18://会议提醒
                extras.setClass(context, WorkMeetingActivity.class);
                extras.putExtra("title", "会议助手");
                extras.putExtra("push", true);
                startActivity(extras);
                finish();
                break;

            case 20://因公外出
                extras.setClass(context, ApplyForBusinessTripActivity.class);
//                extras.putExtra("title", "因公外出申请");
                extras.putExtra("push", true);
                startActivity(extras);
                finish();
                break;


            case 300:
                break;
            case 301://好友赛
                //FIXME 需要传ID 的地方请用msgId
                extras.setClass(context, FriendPKDetailActivity.class);
                extras.putExtra("matchId", msgId);
                extras.putExtra("push", true);
                startActivity(extras);
                finish();
                break;
            case 302://团赛
                extras.setClass(context, GroupPKDetailActivity.class);
                extras.putExtra("matchId", msgId);
                Log.i("darren","msgId:"+msgId);
                extras.putExtra("push", true);
                extras.putExtra("isNeedStep", 0);
                startActivity(extras);
                finish();
                break;
            case 303://竞赛
                extras.setClass(context, GroupPKDetailActivity.class);
                extras.putExtra("matchId", msgId);
                Log.i("darren", "msgId:" + msgId);
                extras.putExtra("push", true);
                extras.putExtra("isNeedStep", 1);
                startActivity(extras);
                finish();
                break;
            case 304:
                extras.setClass(context, GroupPKDetailActivity.class);
                extras.putExtra("matchId", msgId);
                Log.i("darren", "msgId:" + msgId);
                extras.putExtra("push", true);
                extras.putExtra("isNeedStep", 1);
                startActivity(extras);
                finish();
                break;
            default:
                isRunning = getIntent().getBooleanExtra("Running", false);

                if (Util.isEmpty(Const.phone_number)) {
                    UserBean user = UserDao.getInstance(this).getLastLoginUser();
                    Const.phone_number = user.getAccount();
                    Const.login_key = user.getKey();
                }

                new QryMsgByIdAction(this, msgId, this).start();

                break;
        }

        //		info = new MessageInfo();
        //		info.setMsgId(479);
        //		info.setShortMsg("asdasdadas");
        //		info.setMsgContent("XXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
        //		info.setMsgViewId(12111);
        //		info.setIsRead("n");
        //		info.setMsgType(4);
        //		myHandler.sendEmptyMessageDelayed(1, 1000);

    }

    @Override
    public void OK(Object obj) {
        info = (MessageInfo) obj;
        myHandler.sendEmptyMessage(1);
    }

    @Override
    public void ERROR(int resultCode, String message) {
        myHandler.sendEmptyMessage(2);
    }

    @Override
    public void HTTPERROR(int statusCode) {
        myHandler.sendEmptyMessage(2);
    }

    void toShelfActivity(MessageInfo msgInfo) {
        Intent notificationIntent = new Intent();

        if (Util.isNotEmpty(msgInfo.getAddressUrl())) {
            notificationIntent.setClass(context, CommonWebActivity.class);
            notificationIntent.putExtra(DefaultConsts.req_url_s, msgInfo.getAddressUrl());
            notificationIntent.putExtra(DefaultConsts.title_s, msgInfo.getTitle());
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        } else {
            System.out.println("PushService type:" + msgInfo.getMsgType());
            switch (msgInfo.getMsgType()) {
                case MessageInfo.TYPE_BOOK:
                    notificationIntent.setClass(context, NewBookDetailActivity.class);
                    //TODO 通知栏书籍跳转
                    if (msgInfo.getBooks() != null && msgInfo.getBooks().get(0) != null) {
                        notificationIntent.putExtra("BookInfo", msgInfo.getBooks().get(0));
                    }
                    break;
                case MessageInfo.TYPE_URL:

                    break;
                case MessageInfo.TYPE_BANNER:
                    break;
                case MessageInfo.TYPE_SYSTEM:
                    notificationIntent.setClass(context, SystemNotiActivity.class);
                    notificationIntent.putExtra(DefaultConsts.message_content_s, msgInfo.getMsgContent());
                    notificationIntent.putExtra(DefaultConsts.title_s, "消息内容");
                    break;
                case MessageInfo.TYPE_LIVES:
                    break;
                case MessageInfo.TYPE_ORDER:
                    //预约订座通知栏消息点击事件
//					notificationIntent.setClass(context, MyAppointmentListActivity.class);
                    break;
                case MessageInfo.TYPE_COUPON:
                    //赠送代金券通知栏消息点击事件
                    notificationIntent.setClass(context, OrderMenuMyCouponActivity.class);
                    break;

            }
        }
        notificationIntent.putExtra("Running", isRunning);
        notificationIntent.putExtra("MsgId", msgInfo.getMsgViewId());
        notificationIntent.putExtra("UserID", Const.phone_number);
        if (Util.isNotEmpty(msgInfo.getIsRead()) && "n".equals(msgInfo.getIsRead().toLowerCase())) {
            notificationIntent.putExtra(DefaultConsts.message_send_id, msgInfo.getMsgViewId());
        }
        startActivity(notificationIntent);
        finish();
    }
}
